
package com.hjm.project.ShopOrder.ShopOrder.utils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtils {
	
private static ObjectMapper mapper = new ObjectMapper();
	
	
	
	
	/**
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 * 
	* @Title: jsonToMap  
	* @Description: json转换map
	* @param @param json
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public static  Map<String, Object> jsonToMap(String json) throws Exception{
			return  mapper.readValue(json, Map.class);
	}
	
	
	@SuppressWarnings("unchecked")
	public static  Map<String, String> toMap(String json) throws Exception{
			return  mapper.readValue(json, Map.class);
	}
	
	
	
	/**
	 * 
	 *********************************************************.<br>
	 * [方法] jsonToList <br>
	 * [描述] (转换json数组 为map) <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] List<String> <br>
	 * [作者] Gd
	 *********************************************************.<br>
	 */
	@SuppressWarnings("unchecked")
	public static  List<Map<String, Object>> jsonToList(String json) throws Exception{
			return  mapper.readValue(json,List.class);
	}
	
	/**
	 * @throws Exception 
	 * 
	  * 
	  * ********************************
	  * 方法名称:jsonArrayToMap 
	  * 参数:@key json的顶层 key oneKey
	  * 参数:@key 依次获取的key 如  message[age:1]
	  * 描述:(Json数组转换map)
	  * 返回类型:@Map<String,Object>
	  * 作者:Gd
	  * @throws
	  * ********************************
	 */
	public static Map<String, Object> jsonArrayToMap(String json,String oneKey,String twoKey) throws Exception{
		Map<String, Object> data = new HashMap<String, Object>();
		data = jsonToMap(json);
		if (oneKey != null  && twoKey != null) {
			data =  (Map<String, Object>)data.get(oneKey);
			return data = (Map<String, Object>)data.get(twoKey);
		}
		return (Map<String, Object>)data.get(oneKey);
		
	}
	
	
	/**
	 * 
	* @Title: toJson  
	* @Description: (字符串转换为json)
	* @param @param str
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	public static String toJson(Object str){
		try {
			return mapper.writeValueAsString(str);
		} catch (JsonProcessingException e) {
		}
		return null;
	}
	
	
	
	/**
	 * 
	 *********************************************************.<br>
	 * [方法] getBean <br>
	 * [描述] (转换json为对象) <br>
	 * [参数] (对参数的描述) <br>
	 * [返回] Object <br>
	 * [作者] Gd
	 *********************************************************.<br>
	 */
	public  static <T> Object getBean(String json,Class<T> class1){
		try {
			mapper.readValue(json, class1);
		} catch (JsonParseException e) {
		} catch (JsonMappingException e) {
		} catch (IOException e) {
		}
		return null;
	}
	

}

	